import { SingletonBase } from "./SingletonBase";
import { BaseConfig, ConfigClass } from "../Base/BaseConfig";

export class ConfigManager extends SingletonBase{
    private configList:BaseConfig[]=[];
    private curLoadedCount:number=0;

    public loadAllConfig<T extends BaseConfig>(configClass:ConfigClass<T>[],callback?:Function):void {
        configClass.forEach((v,i)=>{
            this.loadConfig(v,this.callback,callback);
        });
    }

    public getConfig<T extends BaseConfig>(configClass:ConfigClass<T>):BaseConfig{
        for(let i=0;i<this.configList.length;++i){
            if(this.configList[i].tag==configClass){
                return this.configList[i];
            }
        }
        return null;
    }

    private loadConfig<T extends BaseConfig>(configClass:ConfigClass<T>,callback:Function,arg:any=null){
        let config=new configClass(callback,this,arg);
        config.tag=configClass;
        this.configList.push(config);
    }

    private callback(callback:Function){
        this.curLoadedCount+=1;
        if(this.configList.length==this.curLoadedCount){
            if(callback){
                callback();
            }
        }
    }
}
